<script setup>
import PageContainer from './components/PageContainer.vue'
import { artGetChannelService, artDelChannelService } from '@/api/article'
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { ElMessageBox } from 'element-plus'

const channel = ref([])
const loadding = ref(false)
const dialog = ref()
const title = ref('')
const getChannel = async () => {
  loadding.value = true
  const res = await artGetChannelService()
  channel.value = res.data.data
  loadding.value = false
}
getChannel()
const handleEdit = (row) => {
  dialog.value.open(row)
}
const handleDelete = async (row) => {
  await ElMessageBox.confirm('你确定删除吗', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannel()
}
const onAddChannel = () => {
  dialog.value.open({})
  title.value = '添加分类'
}
const onSuccess = () => {
  getChannel()
}
</script>
<template>
  <PageContainer title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <template #default>
      <el-table v-loadding="loadding" :data="channel" style="width: 100%">
        <el-table-column type="index" label="编号" width="200" />
        <el-table-column prop="cate_name" label="名称" />
        <el-table-column prop="cate_alias" label="别名" />
        <el-table-column prop="cate_alias" label="操作" width="200">
          <template #default="{ row }">
            <el-button
              type="primary"
              :icon="Edit"
              circle
              plain
              size="small"
              @click="handleEdit(row)"
            ></el-button>
            <el-button
              plain
              size="small"
              type="danger"
              :icon="Delete"
              circle
              @click="handleDelete(row)"
            ></el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据" />
        </template>
      </el-table>
      <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
    </template>
  </PageContainer>
</template>

<style></style>
